package com.dylan.magic.mybatis.multi.config.db;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @author : ankang
 * @date : 2019/12/12 21:44
 * @Description :
 */
@Configuration
@MapperScan(basePackages = "com.dylan.magic.mybatis.multi.storage.goods.mapper", sqlSessionFactoryRef = "sqlSessionFactoryPecker0")
public class Pecker0DbConfig {

    @Bean(name = "dataSourcePecker0")
    @ConfigurationProperties(prefix = "spring.datasource.pecker0")
    @Primary
    public DataSource dataSourcePecker0() {
        DruidDataSource dataSource = DataSourceBuilder.create().type(DruidDataSource.class).build();
        return dataSource;
    }

    @Bean(name = "sqlSessionFactoryPecker0")
    @Primary
    public SqlSessionFactory sqlSessionFactoryPecker0() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSourcePecker0());
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/goods/*.xml"));
        return factoryBean.getObject();
    }

}
